<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="personId">
        <el-input
          v-model="queryParams.personId"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="管控状态" prop="controlStatus">
        <el-select v-model="queryParams.controlStatus" placeholder="请选择管控状态" clearable>
          <el-option
            v-for="dict in dict.type.monitor_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="是否密接" prop="ifCloseContact">
        <el-select v-model="queryParams.ifCloseContact" placeholder="请选择是否密接" clearable>
          <el-option
            v-for="dict in dict.type.yes_or_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="现住址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入现住址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进入本市时间" prop="lastGetInTime">
        <el-date-picker clearable
          v-model="queryParams.lastGetInTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最后进入本市时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item label="核酸检测单id" prop="lastCheckId">
        <el-input
          v-model="queryParams.lastCheckId"
          placeholder="请输入核酸检测单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="公司" prop="company">
        <el-input
          v-model="queryParams.company"
          placeholder="请输入公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['eim:person:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['eim:person:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['eim:person:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['eim:person:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="personList" ref="dataTable" @selection-change="handleSelectionChange" :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数据编号" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="身份证号" align="center" prop="personId" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="身高" align="center" prop="heigh" />
      <el-table-column label="体重" align="center" prop="weight" />
      <el-table-column label="性别" align="center" prop="sex" >
      <template slot-scope="scope">
        <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
      </template>
      </el-table-column>
      <el-table-column label="现住址" align="center" prop="address" />
      <el-table-column label="是否在本市" align="center" prop="ifIn" >
      <template slot-scope="scope">
        <dict-tag :options="dict.type.yes_or_no" :value="scope.row.ifIn"/>
      </template>
      </el-table-column>
      <el-table-column label="进入本市时间" align="center" prop="lastGetInTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastGetInTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离开本市时间" align="center" prop="lastGetOutTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastGetOutTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进入本市来源" align="center" prop="getInFrom" />
      <el-table-column label="离开本市去向" align="center" prop="getOutTo" />
      <el-table-column label="是否确诊" align="center" prop="ifDiagnosis" >
      <template slot-scope="scope">
        <dict-tag :options="dict.type.yes_or_no" :value="scope.row.ifDiagnosis"/>
      </template>
      </el-table-column>
      <el-table-column label="确诊信息id" align="center" prop="diagnosisId" />
      <el-table-column label="是否密接" align="center" prop="ifCloseContact">
      <template slot-scope="scope">
        <dict-tag :options="dict.type.yes_or_no" :value="scope.row.ifCloseContact"/>
      </template>
      </el-table-column>
      <el-table-column label="密接信息id" align="center" prop="closeContactId" />
      <el-table-column label="管控状态" align="center" prop="controlStatus">
      <template slot-scope="scope">
        <dict-tag :options="dict.type.monitor_type" :value="scope.row.controlStatus"/>
      </template>
      </el-table-column>
      <el-table-column label="核酸检测单id" align="center" prop="lastCheckId" />
      <el-table-column label="是否接种疫苗" align="center" prop="ifVaccines" >
      <template slot-scope="scope">
        <dict-tag :options="dict.type.yes_or_no" :value="scope.row.ifVaccines"/>
      </template>
      </el-table-column>
      <el-table-column label="职业" align="center" prop="work" />
      <el-table-column label="公司" align="center" prop="company" />
      <el-table-column label="公司地址" align="center" prop="workAddr" />
      <el-table-column label="籍贯省份" align="center" prop="originProvince" />
      <el-table-column label="籍贯城市" align="center" prop="originCity" />
      <el-table-column label="籍贯区域" align="center" prop="originArea" />
      <el-table-column label="户籍所在地址" align="center" prop="originAddress" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['eim:person:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['eim:person:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改居民基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="personId">
          <el-input v-model="form.personId" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="身高" prop="heigh">
          <el-input v-model="form.heigh" placeholder="请输入身高" />
        </el-form-item>
        <el-form-item label="体重" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入体重" />
        </el-form-item>
        <el-form-item label="现住址" prop="address">
          <el-input v-model="form.address" placeholder="请输入现住址" />
        </el-form-item>
        <el-form-item label="最后进入本市时间" prop="lastGetInTime">
          <el-date-picker clearable
            v-model="form.lastGetInTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后进入本市时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后离开本市时间" prop="lastGetOutTime">
          <el-date-picker clearable
            v-model="form.lastGetOutTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后离开本市时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后进入本市来源" prop="getInFrom">
          <el-input v-model="form.getInFrom" placeholder="请输入最后进入本市来源" />
        </el-form-item>
        <el-form-item label="最后离开本市去向" prop="getOutTo">
          <el-input v-model="form.getOutTo" placeholder="请输入最后离开本市去向" />
        </el-form-item>
        <el-form-item label="确诊信息id" prop="diagnosisId">
          <el-input v-model="form.diagnosisId" placeholder="请输入确诊信息id" />
        </el-form-item>
        <el-form-item label="密接信息id" prop="closeContactId">
          <el-input v-model="form.closeContactId" placeholder="请输入密接信息id" />
        </el-form-item>
        <el-form-item label="最后核酸检测单id" prop="lastCheckId">
          <el-input v-model="form.lastCheckId" placeholder="请输入最后核酸检测单id" />
        </el-form-item>
        <el-form-item label="职业" prop="work">
          <el-input v-model="form.work" placeholder="请输入职业" />
        </el-form-item>
        <el-form-item label="公司" prop="company">
          <el-input v-model="form.company" placeholder="请输入公司" />
        </el-form-item>
        <el-form-item label="公司地址" prop="workAddr">
          <el-input v-model="form.workAddr" placeholder="请输入公司地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPerson,
  getPerson,
  delPerson,
  addPerson,
  updatePerson,
  toDiagnosis,
  toContact,
  toMonitor
} from '@/api/eim/person'

export default {
  name: "Person",
  dicts: ['sys_idcard_type', 'sys_oper_type', 'sys_common_status', 'monitor_type', 'sys_user_sex','yes_or_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 居民基本信息表格数据
      personList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        personId: null,
        phone: null,
        age: null,
        heigh: null,
        weight: null,
        sex: null,
        address: null,
        ifIn: null,
        lastGetInTime: null,
        lastGetOutTime: null,
        getInFrom: null,
        getOutTo: null,
        alive: null,
        ifDiagnosis: null,
        diagnosisId: null,
        ifCloseContact: null,
        closeContactId: null,
        controlStatus: null,
        lastCheckId: null,
        ifVaccines: null,
        work: null,
        company: null,
        workAddr: null,
        originProvince: null,
        originCity: null,
        originArea: null,
        originAddress: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        personId: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        age: [
          { required: true, message: "年龄不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询居民基本信息列表 */
    getList() {
      this.loading = true;
      listPerson(this.queryParams).then(response => {
        this.personList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        personId: null,
        phone: null,
        age: null,
        heigh: null,
        weight: null,
        sex: null,
        address: null,
        ifIn: null,
        lastGetInTime: null,
        lastGetOutTime: null,
        getInFrom: null,
        getOutTo: null,
        alive: null,
        ifDiagnosis: null,
        diagnosisId: null,
        ifCloseContact: null,
        closeContactId: null,
        controlStatus: 0,
        lastCheckId: null,
        ifVaccines: null,
        work: null,
        company: null,
        workAddr: null,
        originProvince: null,
        originCity: null,
        originArea: null,
        originAddress: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加居民基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPerson(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改居民基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePerson(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPerson(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除居民基本信息编号为"' + ids + '"的数据项？').then(function() {
        return delPerson(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('eim/person/export', {
        ...this.queryParams
      }, `person_${new Date().getTime()}.xlsx`)
    },
    /** 转为确诊按钮操作 */
    handleDiagnosis(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认将"' + ids + '"转为确诊病例？').then(function() {
        return toDiagnosis(ids);
      }).then(() => {
        this.$modal.msgSuccess('"' + ids + '"已转为确诊病例');
        this.getList();
      }).catch(() => {});
    },
    /** 转为密接按钮操作 */
    handleContact(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认将"' + ids + '"转为密切接触者？').then(function() {
        return toContact(ids);
      }).then(() => {
        this.$modal.msgSuccess('"' + ids + '"已转为密切接触者');
        this.clearSelection();
      }).catch(() => {});
    },
    /** 转为居家监测按钮操作 */
    handleMonitor(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认将对"' + ids + '"进行居家监测？').then(function() {
        return toMonitor(ids);
      }).then(() => {
        this.$modal.msgSuccess('"' + ids + '"已转为居家监测对象');
        this.clearSelection();
      }).catch(() => {});
    },
    /** 病例显示黄色，居家显示绿色 */
    tableRowClassName({row, rowIndex}) {
      if (row.ifDiagnosis == 1) {
        return 'diagnosis-row';
      } else if (row.controlStatus != 0) {
        return 'control-row';
      }
      return '';
    },
    /** 取消选择行 */
    clearSelection() {
        this.$refs.dataTable.clearSelection();
    },
  }
};
</script>
<style>
.el-table .diagnosis-row {
  background: oldlace;
}

.el-table .control-row {
  background: #f0f9eb;
}
</style>
